﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using NC.Infrastructure;
using NC.Infrastructure.Data;
using NC.Infrastructure.Domain.Identity;
using NC.Infrastructure.DomainDto;
using domainEntity = NC.Infrastructure.Domain.Identity.Person;

namespace NC.Service.Person
{
    public class PersonService : IPersonService
    {
        private readonly IRepository<domainEntity> _personRepository;
        private readonly IRepository<TargetPerson> _targetPersonRepository;
        private readonly IRepository<Cather.Target> _targetRepository;

        public PersonService(IRepository<domainEntity> personRepository ,IRepository<TargetPerson> targetPersonRepository,IRepository<Cather.Target> targetRepository  )
        {
            this._personRepository = personRepository;
            this._targetRepository = targetRepository;
            this._targetPersonRepository = targetPersonRepository;
        }

        public domainEntity Get(int id)
        {
            return _personRepository.Get(id);
        }

        public IQueryable<domainEntity> GetAll()
        {
            return _personRepository.Entities;
        }

        public IQueryable<domainEntity> Get(Expression<Func<domainEntity, bool>> filter)
        {
            return _personRepository.Get(filter);
        }

        public void Insert(domainEntity entity)
        {
            _personRepository.Insert(entity);
        }

        public void Update(domainEntity entity)
        {
            _personRepository.Update(entity);
        }

        public void Delete(domainEntity entity)
        {
            _personRepository.Delete(entity);
        }

        public IQueryable<SimpleDto> SimpleDtos()
        {
            return _personRepository.Entities.Select(n => new SimpleDto()
            {
                Id = n.Id,
                Value = n.Name
            });
        }

        public IQueryable<Cather.Target> GetTargets(int personId)
        {
            var result = from n in _targetPersonRepository.Entities
                join m in _targetRepository.Entities on n.TargetId equals m.Id
                where n.PersonId == personId
                select m;
            return result;
        }

        public void Commit()
        {
            _personRepository.Commit();
        }
    }
}
